<template>
  <div>
    <el-container>
      <el-main>
        <div class="enter-container">
          <div class="top-text">
            <i class="el-icon-user" />
            <span> 登录</span>
          </div>
          <el-form
            ref="form"
            label-position="right"
            :model="form"
            :rules="rules"
            label-width="180px"
            @keyup.enter.native="submitForm"
          >
            <el-form-item label="账号：" prop="userAccount">
              <el-input v-model="form.userAccount" placeholder="账号/邮箱" />
            </el-form-item>
            <el-form-item label="密码：" prop="userPassword">
              <el-input
                v-model="form.userPassword"
                placeholder="密码"
                show-password
              />
            </el-form-item>
            <el-form-item label="验证码：" prop="code">
              <el-row>
                <el-col :span="14">
                  <el-input
                    v-model="form.code"
                    class="verify"
                    style="margin-right: 100px"
                    placeholder="右侧图形验证码"
                  />
                </el-col>
                <el-col :span="10">
                  <div class="login-code" @click="refreshCode">
                    <s-identify :identify-code="identifyCode" />
                  </div>
                </el-col>
              </el-row>
            </el-form-item>
          </el-form>
          <el-button
            type="primary"
            style="width: 240px"
            @click="submitForm"
          >登陆</el-button>
          <el-row style="width:100%; margin: 20px auto">
            <el-col :span="11">
              <el-link
                style="font-size: 16px !important"
                :underline="false"
                type="primary"
                @click="jumpRegister"
              >注册账号
              </el-link>
            </el-col>
            <el-col :span="11" :offset="1">
              <el-link
                style="font-size: 16px !important"
                :underline="false"
                type="primary"
                @click="jumpForget"
              >找回密码
              </el-link>
            </el-col>
          </el-row>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import SIdentify from '@/components/SIdentify'
import { verifyMixin } from '@/common/mixins'
import { login } from '@/networks/enter'

export default {
  name: 'Login',
  components: { SIdentify },
  mixins: [verifyMixin],
  data() {
    return {
      form: {
        userAccount: '',
        userPassword: '',
        code: ''
      },
      rules: {
        userAccount: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.refreshCode()
    this.form.code = ''
  },
  activated() {
    this.refreshCode()
    this.form.code = ''
  },
  methods: {
    jumpRegister() {
      this.$router.push('/register')
    },
    jumpForget() {
      this.$router.push('/forget')
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (!valid) return false
        if (this.form.code !== this.identifyCode) {
          this.$message.error('验证码错误')
          this.refreshCode()
          return false
        }
        login(this.form).then((res) => {
          const data = res.data
          if (data.status === 1) {
            this.$message.success(data.msg)
            // 把token和user存进store 并且放进localStorage
            this.$store.commit('set', {
              token: data.data.token,
              user: data.data.user
            })
            this.$router.push(this.$route.query.redirect || '/')
          } else {
            this.$message.error(res.data.msg)
            this.refreshCode()
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.el-container {
  width: 900px;
  margin-left: auto !important;
  margin-right: auto !important;
}

.el-main {
  text-align: center;
}

.el-row {
  line-height: 40px;
  margin-bottom: 20px;
}

.enter-container {
  height: 500px;
  margin-left: auto !important;
  margin-right: auto !important;
  width: 600px;
  background-color: #ffffff;
}

.top-text {
  font-size: 30px;
  height: 150px;
  line-height: 150px;
}

.login-code {
  cursor: pointer;
}

div /deep/ .el-form-item__content {
  margin-right: 120px;
}
</style>
